package com.letu.core.converter;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class PageDeserializer extends StdDeserializer<Page> {
    public static final PageDeserializer PAGEINSTANCE = new PageDeserializer(Page.class);
    protected PageDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public Page deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        String s = node.toString();
        ObjectMapper om = new ObjectMapper();
        Page page = om.readValue(s, Page.class);
        return page;
    }
}
